package com.key.modules.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.key.common.base.RespResult;
import com.key.modules.dto.DriverAddDTO;
import com.key.modules.dto.DriverEditDTO;
import com.key.modules.entity.Driver;
import com.key.modules.mapper.CarrierMapper;
import com.key.modules.service.DriverService;
import com.key.modules.mapper.DriverMapper;
import com.key.modules.vo.DriverDetailsVO;
import com.key.modules.vo.DriverVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author wei
* @description 针对表【driver(司机表)】的数据库操作Service实现
* @createDate 2023-03-23 19:32:27
*/
@Service
@Slf4j
public class DriverServiceImpl implements DriverService{

    @Autowired
    CarrierMapper carrierMapper;

    @Autowired
    DriverMapper driverMapper;

    @Override
    public RespResult findDriverList(Integer currentPage, Integer pageSize, String dType,
                                     String dTelNum, String dName, String dAuditStatus) {
        log.info("进入findDriverList方法，查询司机表（支持条件查询）"+
                "，currentPage = "+currentPage+
                "，pageSize = "+pageSize+
                "，dType = "+dType+
                "，dTelNum = "+dTelNum+
                "，dName = "+dName+
                "，dAuditStatus = "+dAuditStatus);

        Page<Driver> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Driver> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(StringUtils.isNotEmpty(dType), Driver::getDType, dType)
                .eq(StringUtils.isNotEmpty(dTelNum), Driver::getDTelNum, dTelNum)
                .eq(StringUtils.isNotEmpty(dName), Driver::getDName, dName)
                .eq(StringUtils.isNotEmpty(dAuditStatus), Driver::getDAuditStatus, dAuditStatus);

        Page<Driver> driverPage = driverMapper.selectPage(page, lambdaQueryWrapper);

        List<Driver> driverList = driverPage.getRecords();

        List<DriverVO> driverVOList = new ArrayList<>();
        for(Driver driver : driverList){
            DriverVO driverListVO = BeanUtil.copyProperties(driver, DriverVO.class);
            driverListVO.setCName(carrierMapper.selectCNameByCId(driver.getCId()));
            driverVOList.add(driverListVO);
        }

        log.info("findDriverList方法结束");
        return RespResult.success((int) driverPage.getTotal(), driverVOList);
    }

    @Override
    public DriverDetailsVO findDriverDetails(Integer dId) {
        log.info("进入findDriverDetails方法，查询司机详情"+
                "，dId = "+dId);

        Driver driver = driverMapper.selectById(dId);

        DriverDetailsVO driverDetailsVO = BeanUtil.copyProperties(driver, DriverDetailsVO.class);

        driverDetailsVO.setCName(carrierMapper.selectCNameByCId(driver.getCId()));

        log.info("findDriverDetails方法结束");
        return driverDetailsVO;
    }

    @Override
    public boolean addDriver(DriverAddDTO driverAddDTO) {
        log.info("进入addDriver方法，添加司机"+
                "，driverAddDTO = "+driverAddDTO);

        Driver driver = BeanUtil.copyProperties(driverAddDTO, Driver.class);
        driver.setCId(carrierMapper.selectCIdByCName(driverAddDTO.getCName()));
        int result = driverMapper.insert(driver);

        log.info("addDriver方法结束");
        return result > 0;
    }

    @Override
    public boolean removeDriver(Integer dId) {
        log.info("进入removeDriver方法，删除司机"+
                "，dId = "+dId);

        int result = driverMapper.deleteById(dId);

        log.info("removeDriver方法结束");
        return result > 0;
    }

    @Override
    public boolean editDriver(DriverEditDTO driverEditDTO) {
        log.info("进入editDriver方法，修改司机"+
                "，driverEditDTO = "+driverEditDTO);

        Driver driver = BeanUtil.copyProperties(driverEditDTO, Driver.class);
        driver.setCId(carrierMapper.selectCIdByCName(driverEditDTO.getCName()));
        int result = driverMapper.updateById(driver);

        log.info("editDriver方法结束");
        return result > 0;
    }

    @Override
    public boolean examineAndVerify(Integer dId, Byte dAuditStatus) {
        log.info("进入examineAndVerify方法，审核司机"+
                "，dId = "+dId+
                "，dAuditStatus = "+dAuditStatus);

        LambdaUpdateWrapper<Driver> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.set(dAuditStatus != null, Driver::getDAuditStatus, dAuditStatus)
                .eq(dId != null, Driver::getDId, dId);

        int result = driverMapper.update(null, lambdaUpdateWrapper);

        log.info("examineAndVerify方法结束");
        return result > 0;
    }

}




